﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class NotificacionsByGPS : ClassManager.SessionManagerPage
    {
        private void GetAllNotificationsByGPS(string Imei)
        {
            //Notifications
            GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.NotificationsByGps> NotificationsByGpsEntities = GPSTrackingDBV2.Data.DataRepository.NotificationsByGpsProvider.GetByGpsImei(Imei);
            GPSTrackingDBV2.Data.DataRepository.NotificationsByGpsProvider.DeepLoad(NotificationsByGpsEntities, true, GPSTrackingDBV2.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingDBV2.Entities.Notifications));

            NotificationsByGPSGridLiks.DataSource = NotificationsByGpsEntities;
            NotificationsByGPSGridLiks.DataBind();

            //Parameters
            ParametersGrid.DataSource = new System.Data.DataTable();

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //uscGPSSearch.OnAcceptEvent += uscGPSSearch_OnAcceptEvent;
        }

        void uscGPSSearch_OnAcceptEvent(List<Telerik.Web.UI.GridDataItem> rows)
        {
            string Imei = string.Empty;
            
            foreach (Telerik.Web.UI.GridDataItem row in rows)
            {
                Imei = row.Cells[3].Text;
            }

            //Refresh Data
             GetAllNotificationsByGPS(Imei);
        }

        void uscNotificationsByGPSSearch_OnAddNotificationsByGPSEvent(Telerik.Web.UI.GridDataItem row)
        {
            try
            {
                if (row != null)
                {
                    string Imei = string.Empty;//uscGPSSearch.GetImei();
                    if (!string.IsNullOrEmpty(Imei))
                    {
                        //Insert
                        GPSTrackingDBV2.Entities.NotificationsByGps NotificationsByGpsEntity = new GPSTrackingDBV2.Entities.NotificationsByGps();
                        NotificationsByGpsEntity.GpsImei = Imei;
                        NotificationsByGpsEntity.NotificationId = uscNotificationsSearch.GetNotificationsID();

                        GPSTrackingDBV2.Data.DataRepository.NotificationsByGpsProvider.Insert(NotificationsByGpsEntity);

                        //Clear Controls
                        uscNotificationsSearch.ClearControls();

                        //Refresh Data
                        GetAllNotificationsByGPS(Imei);
                    }
                    else
                    {
                        RadWindowManager1.RadAlert("Debes seleccionar un usuario", 330, 180, "Error", null, null);
                    }
                }
            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref NotificationsByGPSGridLiks);
            }
        }



        protected void NotificationsByGPSGridLinks_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                string Imei = string.Empty;//uscGPSSearch.GetImei();
                GPSTrackingDBV2.Entities.NotificationsByGps NotificationsByGpsEntity = ClassManager.InvokeEntityManager.Invoke_NotificationsByGps_Entity(e);
                NotificationsByGpsEntity.OriginalGpsImei = NotificationsByGpsEntity.GpsImei = Imei;
                NotificationsByGpsEntity.OriginalNotificationId = NotificationsByGpsEntity.NotificationId;                
                GPSTrackingDBV2.Data.DataRepository.NotificationsByGpsProvider.Delete(NotificationsByGpsEntity);

                //Refresh Data
                GetAllNotificationsByGPS(Imei);
            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref NotificationsByGPSGridLiks);
            }
        }
    }
}